package com.example.travel.controller;


import com.example.travel.pojo.User;
import com.example.travel.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    //多条件分页查询
    @RequestMapping("page")
    public String userList(
                            //一定要设置默认值，不然参数为空会报错
                            @RequestParam(defaultValue = "1") int pageNum,
                            @RequestParam(defaultValue = "5") int pageSize,
                            String name,
                            String email,
                            Model model){

        User user = new User();
        //设置查询条件
        if (name != null){
            user.setName(name.trim());
        }

        if (email != null){
            user.setEmail(email.trim());
        }

        //查询条件
        model.addAttribute("condition",user);

        PageInfo<User> userPageInfo = userService.PageInfoFindByParam(pageNum, pageSize, user);

        model.addAttribute("page",userPageInfo);

        return "user/list";

    }

    //添加用户信息
    @RequestMapping("add")
    public String addUser(@RequestParam("name") String name,
                          @RequestParam("sex") String sex,
                          @RequestParam("birthday") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birthday,
                          @RequestParam("telephone") String telephone,
                          @RequestParam("email") String email,
                          @RequestParam("username") String userName,
                          @RequestParam("password") String passWord){

        User user = new User();
        user.setName(name);
        user.setSex(sex);
        user.setBirthday(birthday);
        user.setTelephone(telephone);
        user.setEmail(email);
        user.setUserName(userName);
        user.setPassWord(passWord);

        int flag = userService.addUser(user);
        if (flag >= 1){

            return "redirect:page";
        }else {
            return "error";
        }
    }

    //通过uid获取用户原本信息
    @RequestMapping("getInfo/{uid}")
    public String getUserInfoById(@PathVariable("uid") Integer uid,
                                  Model model){


        User user = userService.getUserInfoById(uid);
        model.addAttribute("user",user);

        return "user/edit";
    }

    //更新
    @RequestMapping("update")
    public String updateUser(@RequestParam("uid") Integer uid,
                             @RequestParam("name") String name,
                             @RequestParam("sex") String sex,
                             @RequestParam("birthday") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birthday,
                             @RequestParam("telephone") String telephone,
                             @RequestParam("email") String email,
                             @RequestParam("username") String userName,
                             @RequestParam("password") String passWord){

        User user = new User();
        user.setUid(uid);
        user.setName(name);
        user.setSex(sex);
        user.setBirthday(birthday);
        user.setTelephone(telephone);
        user.setEmail(email);
        user.setUserName(userName);
        user.setPassWord(passWord);

        int flag = userService.updateUser(user);

        if (flag >= 1){
            return "redirect:page";
        }else {
            return "error";
        }
    }

    //操作删除
    @RequestMapping("delete/{uid}")
    public String deleteUser(@PathVariable("uid") Integer uid){

        int flag = userService.deleteUser(uid);
        if (flag >=1){
            return "redirect:/admin/user/page";
        }else {
            return "error";
        }
    }

    //选中删除
    @RequestMapping("deleteAll/{uid}")
    public int deleteAll(@PathVariable("uid") Integer uid){

        int flag = userService.deleteUser(uid);

        return flag;
    }





}
